(ns amjil.core
  (:require [clojurewerkz.quartzite.scheduler :as qs]
            [clojurewerkz.quartzite.triggers :as t]
            [clojurewerkz.quartzite.jobs :as j]
            [clojurewerkz.quartzite.jobs :refer [defjob]]
            [clojurewerkz.quartzite.schedule.simple :as s]
            [clojurewerkz.quartzite.schedule.cron :as cron :refer [schedule cron-schedule]]
            [clojurewerkz.quartzite.schedule.calendar-interval :as calendar]
            [clojurewerkz.quartzite.schedule.daily-interval :as daily-interval])
  (:gen-class))

(declare runjob)

(defjob MainJob
  [ctx]
  ;nil
  (prn "============"))


(defrecord OneJob []
  org.quartz.Job
  (execute [this ctx]
    (prn "bbbbbbbb")))



(defn runjob
  []
  (let [s   (-> (qs/initialize) qs/start)
        job (j/build
              (j/of-type MainJob)
              (j/with-identity (j/key "jobs.noop.40")))
        trigger (t/build
                  (t/start-now)
                  (t/with-identity (t/key "triggers.40"))
                  (t/with-schedule (daily-interval/schedule
                                      ; (cron-schedule "12 57 13 ? * *"))))]
                                      (daily-interval/with-interval-in-minutes 1))))]
   (qs/schedule s job trigger)))

(defjob JobOne
  [ctx]
  (prn "Job One aaaaaaaaaaaaaaaaaaaa"))

(defjob JobTwo
  [ctx]
  (prn "Job Two aaaaaaaaaaaaaaaaaaaa"))

(def qs (-> (qs/initialize) qs/start))

(defn run
  []
  (let [s qs
        job1  (j/build
                (j/of-type JobOne)
                (j/with-identity (j/key "jobs.one.1")))
        trigger1  (t/build
                    (t/start-now)
                    (t/with-identity (t/key "triggers.1"))
                    (t/with-schedule
                      (daily-interval/schedule (daily-interval/with-interval-in-minutes 1))))
                      ; (calendar/schedule (calendar/with-interval-in-days 25))))
        job2  (j/build
                (j/of-type JobTwo)
                (j/with-identity (j/key "jobs.two.1")))
        trigger2  (t/build
                    (t/start-now)
                    (t/with-identity (t/key "triggers.2"))
                    (t/with-schedule
                      (daily-interval/schedule (daily-interval/with-interval-in-minutes 2))))]
   (qs/schedule s job1 trigger1)
   (qs/schedule s job2 trigger2)))


(defn -main
  [& args]
  (println "Hello, World!")
  ; (log/info "aaaa")
  ; (run
  (prn "aaaaaaaaaaaaaa"))
